<?php

namespace JsonLowCode\Form;

use AllowDynamicProperties;
use Closure;
use JsonLowCode\UI\Grid as GridUI;
use JsonLowCode\UI\GridItem;
use JsonLowCode\Form\Abstract\FormAbstract;

/**
 * @property GridUI $element
 * @method GridUI element()
 */
#[AllowDynamicProperties]
class Grid extends FormAbstract
{

    protected array $elements = [];

    protected string $cols = "";

    protected function booted(): void
    {
        $this->element = new GridUI("1 s:2 m:3 l:4 xl:5 2xl:6");
        $this->element->styles(["padding"=>"15px 8px"]);
    }

    public function cols($cols = 24): static
    {
        $this->element->cols($cols);
        return $this;
    }

    /**
     * 设置GridItem
     * @param Closure $children
     * @param int $span
     * @param int $offset
     * @return $this
     */
    public function item(Closure $children, int $span = 1, int $offset = 0): static
    {
        $formBuilder = new Make($this->build);
        call_user_func($children, $formBuilder);
        $item = new GridItem($formBuilder, $span, $offset);
        $this->elements[] = $item;
        return $this;
    }

    public function jsonSerialize(): array
    {
        $this->element->children($this->elements);
        return parent::jsonSerialize();
    }
}
